package mcsm.DataBaseIntegration;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import mcsm.DataBaseInterface.AddOperation;

import Stragety.TotalStragety;

public class AddSumPreferential implements AddOperation {

	private TotalStragety sumPreferential;
	private ArrayList<TotalStragety> sumPres;
	private int totalStraNum;
	
	public AddSumPreferential(TotalStragety sumPreferential) {
		this.sumPres = new ArrayList<TotalStragety>();
		this.sumPreferential = sumPreferential;
		this.sumPres.add(sumPreferential);
	}
	
	public AddSumPreferential(ArrayList<TotalStragety> sumPres) {
		this.sumPres = sumPres;
	}

	@Override
	public Boolean connect(Statement statement) {
		int i = 0;
		for(int j = 0; j < sumPres.size(); j++) {
			sumPreferential = sumPres.get(j);
			try {
				i = statement.executeUpdate(add());
				statement.executeUpdate("update number set sumPreferential = " + totalStraNum);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return (i != 0);
	}

	@Override
	public String add() {
		DataBaseConnect database = DataBaseConnect.getDatabase();
		totalStraNum = database.getSumPreferentialNumber() + 1;
		String id = sumPreferential.getID();
		double sum = sumPreferential.getTotalPrice();
		String giftID = sumPreferential.getGiftID();
		int giftAmount = sumPreferential.getGiftQuantity();
		double discount = sumPreferential.getDiscount();
		String fromDate= sumPreferential.getStartTime();
		String toDate = sumPreferential.getEndTime();
		String desc  = sumPreferential.getDescription();
		String sql = "insert into sumPreferential values('" + id + "', " + sum + ", '" + giftID + "', " + giftAmount + ", " + discount + ", '" + fromDate + "', '" +toDate +"', '" + desc + "')" ;
		return sql;
	}

}
